package com.ltmonitor.web.controller.command;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ltmonitor.entity.AlarmRecord;
import com.ltmonitor.entity.JT809Command;
import com.ltmonitor.entity.TerminalCommand;
import com.ltmonitor.entity.UserInfo;
import com.ltmonitor.entity.VehicleData;
import com.ltmonitor.service.JT808Constants;
import com.ltmonitor.web.util.JsonMessage;

/**
 * 下发监听命令
 * 
 * @author DELL
 * 
 */
@Controller
@RequestMapping("/listenTerminal")
public class ListenTerminalAction extends TerminalCommandAction {

	@RequestMapping("/view.action")
	public String view(long vehicleId, @RequestParam(defaultValue="0") long alarmId, Model m) {
		m.addAttribute("vehicleId", vehicleId);
		m.addAttribute("alarmId", alarmId);
		return "/command/listenTerminal";
	}
	
	@RequestMapping("/getView.action")
	public String getView(Model m) {
		m.addAttribute("message", "加载成功");
		return "/command/listenTerminalNew";
	}
	

	/**
	 *  监听命令 或者电话回拨
	 * @param listenType  监听类型
	 * @param phoneNo  监听电话
	 * @param alarmId 报警处理Id
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/send.action")
	public JsonMessage execute(long vehicleId, int listenType, String phoneNo, @RequestParam(defaultValue="0") int  alarmId) {
		try {
			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_DIAL_BACK);
			tc.setCmdData(listenType + ";" + phoneNo);
			tc.setCmd("" + listenType);
			tc.setVehicleId(vehicleId);
			SendCommand(tc);

			if (alarmId > 0)
				this.processAlarm(alarmId, AlarmRecord.PROCESS_LISTEN,"");

			return json(true, tc.getEntityId());
		} catch (Exception ex) {
			return json(false, ex.getMessage());
		}
	}

/**
 * * author：shinion
	 * date：2018-12-28
	 * 处理单个主动安全报警信息中单个或者多个报警记录的监听指令
 * @param vehicleIds 车辆列表
 * @param listenType 监听类型
 * @param phoneNo 号码
 * @param adasAlarmIds 主动安全报警记录的id列表,为adasAlarm表格中的id索引，表示单条报警记录数据
 * @param alarmIds 主动安全存放在alarm表格中的id索引列表
 * @return
 */
	@ResponseBody
	@RequestMapping("/sendAdasAlarmListens.action")
	public JsonMessage sendAdasAlarmListens(long[] vehicleIds, int listenType, String phoneNo,long[] adasAlarmIds,long[] alarmIds ) {
		try {
			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_DIAL_BACK);
			tc.setCmdData(listenType + ";" + phoneNo);
			tc.setCmd("" + listenType);
			//定义链表保存返回的命令id
			List idList = new ArrayList();
			if(vehicleIds.length > 0) {
				//循环发送命令
				
				for(long vid:vehicleIds) {
					tc.setVehicleId(vid);
					SendCommand(tc);
					idList.add(""+tc.getEntityId());//存放指令id
				}
			}
			if(alarmIds.length > 0 && adasAlarmIds.length>0) {
				this.processAdasAlarms(idList,alarmIds, adasAlarmIds, AlarmRecord.PROCESS_LISTEN,"监听操作");
			}

			return json(true, idList);
		} catch (Exception ex) {
			return json(false, ex.getMessage());
		}
	}

	
	
	/**
	 * author：shinion
	 * date：2018-12-28
	 * 
	 * 处理多个报警的监听命令
	 * @param vehicleIds 车辆id
	 * @param listenType 监听方式
	 * @param phoneNo 监听电话
	 * @param alarmIdsForAdas 主动安全报警的id，为alarm表中的id索引
	 * @param alarmIds 非主动安全报警的id，为alarm表中的id索引
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/sendAllAlarmListens.action")
	public JsonMessage sendAllAlarmListens(long[] vehicleIds, int listenType, String phoneNo,long[] alarmIdsForAdas,long[] alarmIds) {
		try {
			TerminalCommand tc = new TerminalCommand();
			tc.setCmdType(JT808Constants.CMD_DIAL_BACK);
			tc.setCmdData(listenType + ";" + phoneNo);
			tc.setCmd("" + listenType);
			//定义链表保存返回的命令id
			List idList = new ArrayList();
			if(vehicleIds.length > 0) {
				//循环发送命令
				
				for(long vid:vehicleIds) {
					tc.setVehicleId(vid);
					SendCommand(tc);
					idList.add(tc.getEntityId());//存放指令id
				}
			}
			/*
			 *处理多条报警数据 
			 *
			 */
			if(alarmIds.length>0)
			{
				//批量更新报警记录信息
				for(long alarmId:alarmIds) {
					this.processAlarm(alarmId, AlarmRecord.PROCESS_LISTEN,"监听操作");
				}

			}
			//处理多条主动安全报警，每个报警对应不止一个报警记录
			if(alarmIdsForAdas.length > 0) {
				this.processAdasAlarmByAlarmIds(idList,alarmIdsForAdas,AlarmRecord.PROCESS_LISTEN,"监听操作");
			}

			return json(true, idList);
		} catch (Exception ex) {
			return json(false, ex.getMessage());
		}
	}
}
